package com.grakol.algorithm.samples.tests;

import static org.junit.Assert.assertTrue;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

import org.junit.Before;
import org.junit.Test;

import com.grakol.algorithm.samples.Connectivity;

public class TestConnectivity {
	private Connectivity<Integer> tConnectivity;
	
	@Before
	public void setUp() {
		tConnectivity = new Connectivity<Integer>();
	}

	@Test
	public void runTest() {
		String result = tConnectivity.addConn( 3, 4 );
		assertTrue( result.isEmpty() );
		result = tConnectivity.addConn( 8, 0 );
		assertTrue( result.isEmpty() );
		result = tConnectivity.addConn( 4, 8 );
		assertTrue( result.isEmpty() );
		result = tConnectivity.addConn( 3, 0 );
		assertTrue( result.equals( "(3->4->8->0)" ) );
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		BufferedReader reader = null;
		Connectivity<Integer> connectivity = new Connectivity<Integer>();
		try {
			reader = new BufferedReader( new InputStreamReader( System.in ) );
			String input = null;
			while( true ) {
				System.out.print( "$ Enter connection (n-m): " );
				input = reader.readLine().trim();
				if( input.length() == 0 )
					break;
				StringTokenizer tokenizer = new StringTokenizer( input, " -,/" );
				if( tokenizer.countTokens() != 2 ) {
					System.out.println( "# invalid input" );
					continue;
				}
				int first = Integer.valueOf( tokenizer.nextToken() );
				int last = Integer.valueOf( tokenizer.nextToken() );
				String result = connectivity.addConn( first, last );
				if( result.length() == 0 )
					System.out.println( "$ Connection added ..." );
				else 
					System.out.println( "# Connection not added: " + result );
				connectivity.printRegistry();
			}
		} catch( Exception e ) {
			System.err.println( "Error in TestConnectivity: " + e );
			e.printStackTrace( System.err );
		} finally {
			if( reader != null ) {
				try {
					reader.close();
				} catch( Exception e ) {}
			}
		}
	}

}
